//
//  QTHotVC.m
//  QTLifeCircle
//
//  Created by 王琼 on 17/1/15.
//  Copyright © 2017年 徐腾. All rights reserved.
//

#import "QTHotVC.h"

#import "QTLifeCircleNetworkingTool.h"
#import "QTHotTableViewCell.h"
#import "QTNewModel.h"
#import "QTLoopViewModel.h"
#import "YZLoopView.h"
#import <SVProgressHUD.h>
#import <YYModel.h>
#import <Masonry.h>

@interface QTHotVC ()

@property(nonatomic, strong) UIView *loopView
;
@end

@implementation QTHotVC

- (void)viewDidLoad {
    [super viewDidLoad];
    self.tableView.showsVerticalScrollIndicator = NO;
    self.modelList = [NSMutableArray array];
    [self loadData];
    
    //    self.tableView.delegate = self;
    //    self.tableView.dataSource = self;
    [self.tableView registerNib:[UINib nibWithNibName:@"QTHotTableViewCell" bundle:nil] forCellReuseIdentifier:@"cell"];
    self.tableView.tableHeaderView = self.loopView;
    
    
    
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    //    QTCircleTableViewCell * cell = [[[NSBundle mainBundle]loadNibNamed:@"QTCircleTableViewCell" owner:nil options:nil] lastObject];
    QTHotTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
    cell.model = _modelList[indexPath.row];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    
    
    return cell;
}

#pragma mark - Table view data source


//返回模型数组的长度
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    return self.modelList.count;
    //    return 5;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 100;
}


-(void)loadData
{
    NSString* URL = @"http://iosapi.itcast.cn/life/newsData.json.php";
    
    
    
    [[QTLifeCircleNetworkingTool sharedManager]POSTWithURLStr:URL parameter:nil compeletionHandle:^(id responseObject) {
        if(responseObject == nil)
        {
            [SVProgressHUD showErrorWithStatus:@"网络不给力啊"];
            dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                [SVProgressHUD dismiss];
            });
        }else{
            
            [SVProgressHUD showWithStatus:@"正在加载中...."];
            
            dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                [SVProgressHUD dismiss];
            });
            
            NSLog(@"%@",responseObject);
            NSArray* dicArr = responseObject[@"news"];
            
            NSArray* bennerDic = responseObject[@"banner"];
            
            for (NSDictionary* dict in dicArr)
            {
                QTNewModel * model = [ QTNewModel yy_modelWithDictionary:dict];
                
                [self.modelList addObject:model];
                
            }
            
            for(NSDictionary* dict in bennerDic)
            {
                QTLoopViewModel* LoopModel = [QTLoopViewModel yy_modelWithDictionary:dict];
                [self.benModelList addObject:LoopModel];
            }
            
            NSMutableArray *arrM = [NSMutableArray array];
            for(NSDictionary* dict in bennerDic) {
                NSString *url = dict[@"picUrl"];
                [arrM addObject:url];
            }
            //将表头赋值给轮播器
            YZLoopView *headView = (YZLoopView *)self.tableView.tableHeaderView;
            [headView setURLs:arrM titles:nil didSelected:nil];
            
            
            NSLog(@"----%@",self.modelList);
            [self.tableView reloadData];
            
            dispatch_time_t time=dispatch_time(DISPATCH_TIME_NOW, 2*NSEC_PER_SEC);
            dispatch_after(time, dispatch_get_main_queue(), ^{
                [SVProgressHUD dismiss];
            });
            
        }
        
        
    }];
    
}
////设置表头高度
//- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
//{
//    return 200;
//}
//-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
//    return 0.1;
//}

////设置表头内容  写错了
//-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
//{
//    return self.loopView;
//}

- (UIView *)loopView {
    if (_loopView == nil) {
        YZLoopView *loopView = [[YZLoopView alloc] init];
        loopView.frame = CGRectMake(0, 0, 320, 200);
        loopView.backgroundColor = [UIColor cyanColor];
        _loopView = loopView;
        
    }
    return _loopView;
}
@end

            
